﻿// ////////////////////////////////////////////////////////////////////////////
// 
// SWE Case Tool
// 
// Solution:	CaseTool
// Project:		2. CaseTool GUI
// Filename:	FrmCaseTool.cs
// 
// File Created:	09:25 16.05.2013 
// Last Changed:	18:34 05.06.2013
// 
// ////////////////////////////////////////////////////////////////////////////

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace SWE.CaseTool.GUI
{
	/// <summary>
	/// The case tool main window. Displayes all project specific informations.
	/// </summary>
	public partial class FrmCaseTool : Form
	{
		private Project _project;

		/// <summary>
		/// Initializes a new instance of the <see cref="FrmCaseTool" /> class.
		/// </summary>
		public FrmCaseTool() { InitializeComponent(); }

		/// <summary>
		/// Opens the Dialog with for choosing a method.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void BtnCalcClick(object sender, EventArgs e)
		{
			var factory = new MethodFactory(_project);
			using (var wnd = new WndChooseMethod(factory))
			{
				if (wnd.ShowDialog(this) == DialogResult.OK)
				{
					if(wnd.Window != null)
						wnd.Window.ShowDialog(this);
				}
			}
		}

		/// <summary>
		/// Handles the Load event of the CtrlCaseTool control. Loads if possible the last opended project or
		/// creates a new one.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void CtrlCaseToolLoad(object sender, EventArgs e)
		{
			if (!String.IsNullOrWhiteSpace(Settings.GetInstance().LastSavePath))
			{
				try
				{
					_project = ProjectFactory.LoadProject(Settings.GetInstance().LastSavePath);
				}
				catch (Exception ex)
				{
					MessageBox.Show(this, "Konnte das letzte Projekt nicht laden:\n" + ex.Message, "Fehler beim Laden",
					                MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}

			if (_project == null)
			{
				_project = ProjectFactory.CreateProject();
			}

			LoadProject();
		}

		/// <summary>
		/// Writes the project informations in the gui elements.
		/// </summary>
		private void LoadProject()
		{
			tbName.Text = _project.ProjectName;
			pgGeneral.SelectedObject = _project.Information;
		}

		/// <summary>
		/// Handles the Click event of the exitToolStripMenuItem control. Closes the window.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void ExitToolStripMenuItemClick(object sender, EventArgs e) { Close(); }

		/// <summary>
		/// Opens a file dialog and tries to load the selected file.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void LadenToolStripMenuItemClick(object sender, EventArgs e)
		{
			var ofd = new OpenFileDialog
				{
					AddExtension = false,
					CheckFileExists = true,
					DefaultExt = "swe",
					Multiselect = false,
					Filter = @"Projekt Datein *.swe|*.swe|Alle Dateien *.*|*.*"
				};
			if (ofd.ShowDialog(this) == DialogResult.OK)
			{
				try
				{
					_project = ProjectFactory.LoadProject(ofd.FileName);
				}
				catch (Exception ex)
				{
					MessageBox.Show(this, "Konnte das Projekt nicht laden:\n" + ex.Message, "Fehler beim Laden",
					                MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		/// <summary>
		/// Opens a save file dialog and saves the project in the selected file.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void SpeichernToolStripMenuItemClick(object sender, EventArgs e)
		{
			var sfd = new SaveFileDialog
				{
					AddExtension = false,
					CheckFileExists = false,
					CheckPathExists = true,
					FileName = String.IsNullOrWhiteSpace(_project.ProjectName) ? "projekt" : _project.ProjectName,
					DefaultExt = "swe",
					Filter = @"Projekt Datein *.swe|*.swe|Alle Dateien *.*|*.*"
				};

			if (sfd.ShowDialog(this) == DialogResult.OK)
			{
				try
				{
					ProjectFactory.SaveProject(sfd.FileName, _project);
					Settings.GetInstance().LastSavePath = sfd.FileName;
				}
				catch (Exception ex)
				{
					MessageBox.Show(this, "Konnte das Projekt nicht abspeichern:\n" + ex.Message, "Fehler beim Speichern",
					                MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			else
			{
				var args = e as FormClosingEventArgs;
				if (args != null)
				{
					args.Cancel = true;
				}
			}
		}

		/// <summary>
		/// Writes the changed name to project.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void TbNameTextChanged(object sender, EventArgs e) { _project.ProjectName = tbName.Text; }

		/// <summary>
		/// Saves the project in the last saved path.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">
		/// The <see cref="FormClosingEventArgs" /> instance containing the event data.
		/// </param>
		private void CtrlCaseToolFormClosing(object sender, FormClosingEventArgs e)
		{
			if (!string.IsNullOrWhiteSpace(Settings.GetInstance().LastSavePath) &&
			    File.Exists(Settings.GetInstance().LastSavePath))
			{
				try
				{
					ProjectFactory.SaveProject(Settings.GetInstance().LastSavePath, _project);
				}
				catch (Exception ex)
				{
					if (MessageBox.Show(this, "Konnte das Projekt nicht abspeichern:\n" + ex.Message, "Fehler beim Speichern",
					                    MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.Cancel)
					{
						e.Cancel = true;
					}
				}
			}
			else
			{
				if (MessageBox.Show(this, "Sie haben das Projekt nicht gespeichert. Beim beenden gehen alle " +
				                          "Daten verloren, wollen sie das Projekt noch speichern?",
				                    "Nicht gespeicherte Änderungen.", MessageBoxButtons.YesNo) == DialogResult.Yes)
				{
					SpeichernToolStripMenuItemClick(this, e);
				}
			}
		}

		/// <summary>
		/// Opens the settings dialog. Displayes the settings file. It is a simple dialog, with direct data
		/// binding, which makes it ugly and nearly unusable without knowledge about this software, but it
		/// works..
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void EinstellungenToolStripMenuItemClick(object sender, EventArgs e)
		{
			var frm = new Form
				{
					Size = new Size(600, 400),
					FormBorderStyle = FormBorderStyle.SizableToolWindow
				};

			frm.Controls.Add(new PropertyGrid
				{
					SelectedObject = Settings.GetInstance(),
					Dock = DockStyle.Fill
				});

			frm.ShowDialog(this);
		}

		/// <summary>
		/// Opens the dialog for editing the product functions.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void BtnFunctionClick(object sender, EventArgs e) { new FrmList<ProjectItem>(_project.Functions, "Produktfunktionen").ShowDialog(this); }

		/// <summary>
		/// Opens the dialog for editing the product requirements.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void BtnDataClick(object sender, EventArgs e) { new FrmList<ProjectItem>(_project.Requirements, "Produktdaten").ShowDialog(this); }

		/// <summary>
		/// Creates a new empty project.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void NeuToolStripMenuItemClick(object sender, EventArgs e)
		{
			_project = ProjectFactory.CreateProject();
			LoadProject();
			Settings.GetInstance().LastSavePath = null;
		}
	}
}